package com.rin.service;

import com.alibaba.fastjson.JSON;
import com.rin.executor.AnnotationExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author RinMi
 * @since 2023/6/13
 */

@Service
public class UpdateService extends BaseService {

    @Resource
    private AnnotationExecutor annotationExecutor;

    public void update(Object entity, String entityId) {
        if (checkId(entity, entityId) == 0) {
            throw new RuntimeException("不存在");
        }
        annotationExecutor.updateExecutor(entity);
        String destination = createDestination(entity.getClass(), entityId);
        delete(destination);
        //获取json
        String entityJson = JSON.toJSONString(entity);
        //将字符串转化为字节流
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(entityJson.getBytes(StandardCharsets.UTF_8));
        //上传
        upload(byteArrayInputStream, destination);
    }

}
